*** One-Child Policy, Marriage Distortion, and Welfare Loss ***
*** by Wei Huang, Yinghao Pan and Yi Zhou ***

clear all 
set more off
set scheme s1color
cap log close 
graph set window fontface "Times New Roman"

set niceness 10
gl MAIN = "F:\Dropbox"

gl path1 = "$MAIN/Census"
gl path2 = "$MAIN/Marriage/Working_data"
gl path3 = "$MAIN/Marriage/Restat/dataverse_files/Results" 
gl path4 = "$MAIN/Marriage/Restat/dataverse_files/Graphics" 
gl CONTROL = " year_birth sex#age urban_hk prov_hk##c.(year_birth ) "



use "$path2/unmarried", clear
keep if relation == 0 | relation == 1 | relation == 2 | relation == 6 

merge 1:1 wave area hhid name relation using "$path2/min_marr_policy", keepusing(marry_1 marry_2 marry_3)

drop _merge 
drop if mi(hktype)
replace urban = hktype == 1 
su age 

cap gen female = 1-male
cap gen han = 1- minority 

replace unmarried = unmarried * 100 
gen unmarr_25 = marr_age > 25 if !mi(marr_age)
replace unmarr_25 = 1 if unmarried == 100 
replace unmarr_25 = unmarr_25 *100 

gen urban_hk = hktype == 2 

egen clsid = group(prov_hk year_birth)

gen marry_9 = unmarried == 0 & (mi(marry_1) & mi(marry_2) & mi( marry_3))

recode marry_1 marry_2 marry_3 (. = 0) 

gen HH = marry_1 * 100 
gen HM = marry_2 * 100 
gen MM = marry_3 * 100 
gen Other = marry_9 * 100 
gen HOMO = HH + MM
su HOMO

keep if age <= 55 
keep if age > 25
keep if year_birth <= 1980 
keep if Other == 0 


egen fine_16_20 = rowmean(fine_16 fine_17 fine_18 fine_19 fine_20)

replace prov_birth = prov_hk if mi(prov_birth)

gen diff_birth_hk = prov_hk != prov_birth
gen diff_birth_resid = prov != prov_birth
gen diff_hk_resid = prov_hk != prov_birth

**** Appendix Table A12 ****


cap erase "$path3/marriage_fine_mig_prov.xls"
cap erase "$path3/marriage_fine_mig_prov.txt"


foreach sample in "1" {
foreach gender in "1" { 
foreach y_var in  "diff_birth_hk" "diff_birth_resid" "diff_hk_resid"  {
foreach policy in "1" "mino_policy == 1"  "mino_policy == 0"  {
foreach eth in  "1""han" "han == 0"{
foreach age in "16_25"  {
preserve 
keep if `sample' & `policy' & `gender' & `eth'
replace `y_var' = `y_var'*100

reghdfe `y_var' fine_`age' [aw = aw], a($CONTROL)  cluster(prov_hk)
su `y_var' [aw = aw]
outreg2 using "$path3/marriage_fine_mig_prov.xls", append bracket addstat(Y, `r(mean)') ctit(`sample' &`y_var' & `policy' & `gender' & `eth') keep(fine_`age') dec(3)

restore
}
}
}
}
}
}

drop fine_*
ren prov prov_t 

ren prov_birth prov 
forvalues i = 16(1)29{
gen year = year_birth + `i' 
merge m:1 prov year using "$path2/fines_use", keepusing(fine)

drop if _merge == 2
replace fine = 0 if fine == . & year <= 1979
drop _merge year
ren fine fine_birth_`i'
}
ren prov prov_birth 

ren prov_t prov 
forvalues i = 16(1)29{
gen year = year_birth + `i' 
merge m:1 prov year using "$path2/fines_use", keepusing(fine)

drop if _merge == 2
replace fine = 0 if fine == . & year <= 1979
drop _merge year
ren fine fine_resid_`i'
}

egen fine_birth_16_25 = rowmean(fine_birth_16-fine_birth_25)
egen fine_resid_16_25 = rowmean(fine_resid_16-fine_resid_25)



**** Appendix Table A13 ****


cap erase "$path3/marriage_fine_robust_prov.xls"
cap erase "$path3/marriage_fine_robust_prov.txt"


foreach sample in "1" {
foreach gender in "1" { 
foreach y_var in  "unmarried"   {
foreach policy in "1" "mino_policy == 1"  "mino_policy == 0"  {
foreach eth in  "han" "han == 0"{
foreach age in "birth_16_25" "resid_16_25"  {
preserve 
keep if `sample' & `policy' & `gender' & `eth'

reghdfe `y_var' fine_`age' [aw = aw], a($CONTROL)  cluster(prov_hk)
su `y_var' [aw = aw]
outreg2 using "$path3/marriage_fine_robust_prov.xls", append bracket addstat(Y, `r(mean)') ctit(`sample' &`y_var' & `policy' & `gender' & `eth') keep(fine_`age') dec(3)

restore
}
}
}
}
}
}


foreach sample in "unmarried == 0" {
foreach gender in "1" { 
foreach y_var in   "HM"  {
foreach policy in "1" "mino_policy == 1"  "mino_policy == 0"  {
foreach eth in  "han" "han == 0"{
foreach age in "birth_16_25" "resid_16_25"  {
preserve 
keep if `sample' & `policy' & `gender' & `eth'


reghdfe `y_var' fine_`age' [aw = aw], a($CONTROL)  cluster(prov_hk)
su `y_var' [aw = aw]
outreg2 using "$path3/marriage_fine_robust_prov.xls", append bracket addstat(Y, `r(mean)') ctit(`sample' &`y_var' & `policy' & `gender' & `eth') keep(fine_`age') dec(3)

restore


}
}
}
}
}
}

egen n_birth = rowtotal(numbirm numbirf)
replace n_birth = . if age > 45

cap erase "$path3/birth_fine_robust_prov.xls"
cap erase "$path3/birth_fine_robust_prov.txt"


foreach sample in  "Other == 0"{
foreach gender in "sex == 2" {
foreach y_var in "n_birth" {
foreach policy in "1" "mino_policy == 1"  "mino_policy == 0"  {
foreach eth in  "han" "han == 0"{
foreach age in "birth_16_25" "resid_16_25"  {
preserve 
keep if `sample' & `policy' & `gender' & `eth'



reghdfe `y_var' fine_`age'  [aw = aw], a($CONTROL)  cluster(prov_hk)
su `y_var' [aw = aw]
outreg2 using "$path3/birth_fine_robust_prov.xls", append bracket addstat(Y, `r(mean)') ctit(`sample' &`y_var' & `policy' & `gender' & `eth') keep(fine_`age') dec(3)


restore


}
}
}
}
}
}




